/**
 * Program  : PhotoActivity.java
 * Author   : long
 * Create   : 2016年3月18日 上午11:08:21
 *
 * Copyright 2016 by Embedded Internet Solutions Inc.,
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Embedded Internet Solutions Inc.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with Embedded Internet Solutions Inc.
 *
 */

package cn.broadin.app.hotel;

import java.util.List;
import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import android.os.Bundle;
import cn.broadin.app.hotel.util.XDialog;
import cn.broadin.app.hotel.util.XHttpProxy;
import cn.broadin.app.hotel.util.XLog;
import cn.broadin.app.hotel.util.XToast;
import cn.broadin.app.hotel.view.HackyViewPager;
import cn.broadin.app.hotel.view.ImageDetailFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TextView;

/**
 * 照片查看器
 * 
 * @author long
 * @version 1.0.0
 * @date 2016年3月18日上午11:08:21
 */
public class PhotoActivity extends FragmentActivity {

	private static final String STATE_POSITION 		= "STATE_POSITION";
	public static final String EXTRA_IMAGE_INDEX 	= "image_index";
	public static final String EXTRA_IMAGE_PHOTOS 	= "image_photos";
	public static final String EXTRA_IMAGE_URL 		= "image_url";

	private HackyViewPager mPager;
	private int pagerPosition;
	private TextView indicator;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo);
		
		
		pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
		String url = getIntent().getStringExtra(EXTRA_IMAGE_URL);
		List<String> photos = getIntent().getStringArrayListExtra(EXTRA_IMAGE_PHOTOS);

		XLog.i("url.result--DYH: " + url);
		mPager = (HackyViewPager) findViewById(R.id.pager);
		
		if (savedInstanceState != null) {
			pagerPosition = savedInstanceState.getInt(STATE_POSITION);
		}
		
		
		if(photos == null){
			initLoadPhotos(url);
		} else {
			initPhotosUI(photos);
		}
	}
	
	/**
	 * 加载服务器照片数据
	 * 
	 * @author   long
	 * @return void
	 * @exception
	 * @author   long
	 * @since 1.0.0
	 * @date 2015年12月29日 下午7:31:33
	 */
	private void initLoadPhotos(String uri){
		XDialog.getInstance().show(this);
		RequestParams requestParams = new RequestParams();
		HttpUtils httpUtils = XHttpProxy.getHttpUtils();
		httpUtils.send(HttpRequest.HttpMethod.POST, uri, requestParams, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				XDialog.getInstance().hide();
				XToast.makeText(getApplicationContext(), "服务器繁忙，请稍后再试！").show();
			}

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				XLog.i("responseInfo.result: " + responseInfo.result);
				List<String> photos = (List<String>) JSON.parse(responseInfo.result);
				initPhotosUI(photos);
				XDialog.getInstance().hide();
			}
		});
	}
	
	private void initPhotosUI(List<String> photos){
		ImagePagerAdapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), photos);
		mPager.setAdapter(mAdapter);
		indicator = (TextView) findViewById(R.id.indicator);

		CharSequence text = getString(R.string.viewpager_indicator, 1, mPager.getAdapter().getCount());
		indicator.setText(text);
		// 更新下标
		mPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageSelected(int arg0) {
				CharSequence text = getString(R.string.viewpager_indicator, arg0 + 1, mPager.getAdapter().getCount());
				indicator.setText(text);
			}

		});
		mPager.setCurrentItem(pagerPosition);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putInt(STATE_POSITION, mPager.getCurrentItem());
	}

	private class ImagePagerAdapter extends FragmentStatePagerAdapter {

		public List<String> fileList;

		public ImagePagerAdapter(FragmentManager fm, List<String> fileList) {
			super(fm);
			this.fileList = fileList;
		}

		@Override
		public int getCount() {
			return fileList == null ? 0 : fileList.size();
		}

		@Override
		public Fragment getItem(int position) {
			String url = fileList.get(position);
			return ImageDetailFragment.newInstance(url);
		}

	}
}